{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction to the Shell"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Terminal](images/terminal.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## What is the Shell?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The shell is a program that takes commands from the keyboard and gives them to the operating system to perform. \n",
    "\n",
    "It is the outermost layer of the OS, so it is a user interface to access the OS, through a *graphical user interface* (GUI) or through a *command line interface* (CLI).\n",
    "\n",
    "On most linux systems a program called `bash` acts as the Shell program. There are also other Shell programs besides `bash` (like the different browsers we have to interact with the internet)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## What is a Terminal?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The *terminal emulator* is another program that opens a window and lets you interact with the Shell.\n",
    "\n",
    "Just like the Shell, there are a bunch of different *terminal emulator* programs that you can use.\n",
    "\n",
    "The Terminal is synonymous with the *command line* or *command prompt*. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## What are Shell Scripts? "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A shell script is a file containing a series of commands. The shell reads this file and carries out the commands (as if they were entered on the command line).\n",
    "\n",
    "The shell is somewhat unique, in that it is both a powerful **command line interface to the system** and a **scripting language interpreter**. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## What are some basic commands?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Open a terminal in JupyterLab\n",
    "- Some basic commands: `pwd`, `ls`, `cd`, `mkdir`, `echo`, `mv`, `cp`, `cat`, `rm`\n",
    "- Read the docs for each command: `man command` \n",
    "- Tab-completion and history search\n",
    "- Run above commands from Notebook cells: `!pwd`, `!ls`, `!cd` ...\n",
    "- Run commands from a shell script"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the following commands on the Terminal\n",
    "\n",
    "```\n",
    "pwd\n",
    "ls\n",
    "ls -1\n",
    "ls -l\n",
    "ls -a\n",
    "cd ~\n",
    "pwd\n",
    "ls\n",
    "cd -\n",
    "cd ..\n",
    "pwd\n",
    "ls\n",
    "cd -\n",
    "echo 'this text is printed on the screen'\n",
    "echo 'this text is saved to a file' > file.txt\n",
    "echo 'this is another line of text' >> file.txt\n",
    "cat file.txt\n",
    "cp file.txt file2.txt\n",
    "mkdir test\n",
    "mv file2.txt test/file3.txt\n",
    "cat test/file3.txt\n",
    "rm test/*\n",
    "rmdir test\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Read the documentation (manual pages) of each command\n",
    "```\n",
    "man pwd\n",
    "man ls\n",
    "man echo\n",
    "...\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the same commands on a Notebook cell but adding a `!` in front if it   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "( this is the same as running on the Terminal but from a Notebook cell )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pwd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!ls -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!echo 'Hello from the Notebook cell :)'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Another way to run Shell commands from a Notebook cell is using `%%bash`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "\n",
    "# Dump the output of ls to a file \n",
    "ls -l >> output.txt\n",
    "\n",
    "# Show me the last 3 lines of the file\n",
    "tail -3 output.txt\n",
    "\n",
    "# Pipe the output of cat to wc, and count lines\n",
    "cat output.txt | wc -l\n",
    "\n",
    "# Find the word `commands` in files ending with *.txt\n",
    "grep -H commands *.txt\n",
    "\n",
    "# Remove the file we created\n",
    "rm output.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now run all the commands as a Shell script"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!cat commands.sh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!sh commands.sh"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Bash Shell Reference\n",
    "\n",
    "[Cheatsheet](https://courses.cs.washington.edu/courses/cse390a/14au/bash.html) with a summary of the most common Bash commands."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
